﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Text;
using System.Collections.Specialized;
using System.Windows.Forms;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Framework.Client;
using Microsoft.TeamFoundation.Framework.Common;

namespace TFSRequestManager
{
    public static class Utility
    {
        private const string SERVICE_URI_SUFFIX = "/teamfoundation/administration/v3.0/administrationservice.asmx";

        #region Utility Methods

        public static void CopyNotificationsToClipboard(NameValueCollection notificationMessages)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("Date\tMessage\r\n");
            foreach (string key in notificationMessages.AllKeys)
            {
                sb.AppendFormat("{0}\t{1}\r\n", key, notificationMessages[key]);
            }

            Clipboard.SetText(sb.ToString());
        }

        public static void CopyActiveRequestsToClipboard(IEnumerable<ActiveRequest> activeRequests)
        {
            StringBuilder sb = new StringBuilder();

            foreach (var activeRequest in activeRequests)
            {
                sb.AppendFormat("{0}\r\n", activeRequest.ToString());
            }

            Clipboard.SetText(sb.ToString());
        }

        public static Guid[] GetHostIdFromUri(string hostUri)
        {
            List<Guid> guidList = new List<Guid>();

            TfsConfigurationServer tfsConfigurationServer = TfsConfigurationServerFactory.GetConfigurationServer(new Uri(hostUri));

            guidList.Add(tfsConfigurationServer.InstanceId);

            CatalogNode configurationServerNode = tfsConfigurationServer.CatalogNode;

            // Query the children of the configuration server node for all of the team project collection nodes
            ReadOnlyCollection<CatalogNode> tpcNodes = configurationServerNode.QueryChildren(
                    new Guid[] { CatalogResourceTypes.ProjectCollection },
                    false,
                    CatalogQueryOptions.None);

            foreach (CatalogNode tpcNode in tpcNodes)
            {
                Guid tpcId = new Guid(tpcNode.Resource.Properties["InstanceId"]);
                guidList.Add(tpcId);
            }

            return guidList.ToArray();
        }

        public static string GetTfsHostNameFromServiceUri(string serviceUri)
        {
            int hostNameEndIndex = serviceUri.ToLowerInvariant().IndexOf(SERVICE_URI_SUFFIX);
            if (hostNameEndIndex > 0)
            {
                return serviceUri.Substring(0, hostNameEndIndex);
            }
            return string.Empty;
        }

        public static string GetTfsHostNamesForDisplay(string[] remoteAddresses)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < remoteAddresses.Length; i++)
            {
                sb.AppendFormat("{0}{1}", remoteAddresses[i].ToLowerInvariant().Replace(SERVICE_URI_SUFFIX, string.Empty),
                    i < (remoteAddresses.Length - 1) ? "," : string.Empty);
            }
            return sb.ToString();
        }

        public static string[] GetRemoteAddresses()
        {
            string keyNameFormat = "Url{0}";
            bool keyFound = false;
            List<string> addresses = new List<string>();
            int counter = 0;

            do
            {
                string keyValue = ConfigurationManager.AppSettings[string.Format(keyNameFormat, counter)] as string;
                if (keyValue != null)
                {
                    addresses.Add(keyValue);
                    keyFound = true;
                    counter++;
                }
                else
                {
                    keyFound = false;
                }
            } while (keyFound);

            return addresses.ToArray();
        }

        #endregion
    }
}
